/*
 * @Author: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @Date: 2025-08-22 05:39:07
 * @LastEditors: 来自火星的码农 15122322+heyzhi@user.noreply.gitee.com
 * @LastEditTime: 2025-10-03 14:49:52
 * @FilePath: /GateServer/src/Cserver.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "Cserver.h"
#include "httpconnection.h"
#include <iostream>
#include <boost/asio/io_context.hpp>
#include <boost/beast/core/error.hpp>
#include <memory>
#include "../util/logger.h"
#include "asioservicepool.h"
namespace Yizhi {
   CServer::CServer(boost::asio::io_context &ioctx,unsigned short&port)
   :m_ioctx(ioctx)
   ,m_socket(ioctx)
   ,m_acceptor(ioctx,tcp::endpoint(tcp::v4(),port)){

   }

   void CServer::Start(){
        LOG_DEBUG("Cserver start");
        auto self=shared_from_this();
        auto& ioctx=AsioServicePool::GetInstance()->GetIoservice();
        auto new_con=std::shared_ptr<HttpConnection>(new HttpConnection(ioctx));
        m_acceptor.async_accept(new_con->getsokcet(),[self,new_con](beast::error_code err){
            try {
                if(err){
                    self->Start();
                    return ;
                }
                
                LOG_INFO("connect successs");
                new_con->start();
                self->Start();
            } catch (std::exception& e) {
                LOG_ERROR(e.what());
                self->Start();
            }
        });
   }
}